/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 *
 * @author Obuhov D.G.
 */
public class CharsetFilter implements Filter {

    // кодировка
    private String encoding;

    public CharsetFilter() {
    }

    /**
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {

        request.setCharacterEncoding(encoding);
        chain.doFilter(request, response);

    }

    /**
     * Destroy method for this filter
     */
    @Override
    public void destroy() {
    }

    /**
     * Init method for this filter
     */
    @Override
    public void init(FilterConfig filterConfig) {
        // читаем из конфигурации
        encoding = filterConfig.getInitParameter("requestEncoding");

        // если не установлена - устанавливаем Cp1251
        if (encoding == null) {
            encoding = "Cp1251";
        }
    }
}
